package com.ddch.udpsocket.util;

import android.content.Context;
import android.net.DhcpInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.lang.reflect.Method;
import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;

/**
 * created by 韦敏敏
 * on 2019/12/18
 */
public class WifiUtil {
    private static final String TAG = "liaoming";
    private static volatile WifiUtil instance = null;
    private WifiManager mWifiManager;
    private Context mContext;
    private WifiUtil(Context context){
        this.mContext = context;
        mWifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
    }
    public static WifiUtil getInstance(Context context){
        if(instance == null){
            synchronized (WifiUtil.class){
                if(instance == null){
                    instance = new WifiUtil(context);
                }
            }

        }
        return instance;
    }

    /**
     * 优先获取网卡地址
     * @return
     */
    public static String getBroadcastAddress() {
        String broardcast = getBroardcastAddress("p2p");
        if(broardcast == null){
            return getBroardcastAddress("wlan0");
        }
        return broardcast;
    }

    private static String getBroardcastAddress(String netCardName) {
        try {
            Enumeration<NetworkInterface> eni = NetworkInterface.getNetworkInterfaces();
            while(eni.hasMoreElements()){
                NetworkInterface networkCard = eni.nextElement();
                if(networkCard.getDisplayName().startsWith(netCardName)){
                    List<InterfaceAddress> ncAddrList = networkCard.getInterfaceAddresses();
                    Iterator<InterfaceAddress> ncAddrIterator = ncAddrList.iterator();
                    while(ncAddrIterator.hasNext()){
                        InterfaceAddress networkCardAddress = ncAddrIterator.next();
                        InetAddress address = networkCardAddress.getAddress();
                        if(!address.isLoopbackAddress()){
                            String hostAddress = address.getHostAddress();
                            if(hostAddress.indexOf(":") > 0){
                                // case : ipv6
                                continue;
                            } else {
                                // case : ipv4
                                String broardcastAddress = networkCardAddress.getBroadcast().getHostAddress();
                                return broardcastAddress;
                            }
                        }

                    }
                }

            }
        } catch (SocketException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean isWifiApEnabled(){
        try {
            Method method = mWifiManager.getClass().getMethod("isWifiApEnabled");
            method.setAccessible(true);
            return (boolean) method.invoke(mWifiManager);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
    public String getLocalIPAddress(){
        WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
        return intToIp(wifiInfo.getIpAddress());
    }

    private String intToIp(int i) {
        return (i & 0xFF) + "." + ((i >> 8) & 0xFF) + "." + ((i >> 16) & 0xFF) + "."
                + ((i >> 24) & 0xFF);
    }
    public String getServerIPAddress(){
        DhcpInfo mDhcpInfo = mWifiManager.getDhcpInfo();
        return intToIp(mDhcpInfo.gateway);

    }
}
